import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { Body, _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ChangeEditComponent } from '../change-edit/change-edit.component';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';
import { ACLService } from '@delon/acl';


@Component({
  selector: 'change-list',
  templateUrl: './change-list.component.html',
  styles: []
})
export class ChangeListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  syncFlag:boolean = this.aclService.canAbility("change-manage/sync");//检查用户按钮权限
  changeFlag:boolean =  this.aclService.canAbility("change-manage/change");
  unbundingFlag:boolean =  this.aclService.canAbility("change-manage/unbundling");
  selectData: any;
  loading = false;
  addUserInfo = {
    userAccount: '',
    userPassword: '',
    userName: '',
    roles: [],
    depts: []
  };
  config: SPage = {
    columns: [
      {
        index:"",
        title:"选择",
        type:ColumnType.radio
      },
      {
        index: 'deviceCode',
        title: '设备SN码',
        filter: {
          placeholder: '请输入设备SN码',
          type: 'string'
        }
      },
      {
        index: 'deviceDescribe',
        title: '设备描述',
        filter: {
          placeholder: '请输入设备描述',
          type: 'string'
        }
      },
      {
        index: 'deviceFac',
        title: '设备厂商',
        filter: {
          placeholder: '请输入厂商',
          type: 'string'
        }
      },
      {
        index:'deviceCategory',
        title:'设备类型',
        type: ColumnType.tag,
        tag: {
          1:{text: '大门口机'},
          2:{text: '管理机'},
          3:{text: '单元机'},
          4:{text:'室内机'}
        },
      },
      {
        index: 'deviceType',
        title: '设备分类',
        type: ColumnType.tag,
        tag:{
          1:{text: '门禁摄像机'},
          2:{text: '门禁刷卡'},
          3:{text: 'IPC'},
          4:{text: 'NVR'},
          5:{text: '微型门禁产品'},
          6:{text: '安卓门禁产品'},
          7:{text: '门铃'},
          8:{text: '4G开门控制器'},
          12:{text: 'linux人脸机'}
        },
        filter: {
          placeholder: '请选择设备分类',
          type: 'select',
          items: [
            {label: '门禁摄像机', value: 1},
            {label: '门禁刷卡', value: 2},
            {label: 'IPC', value: 3},
            {label: 'NVR', value: 4},
            {label: '微型门禁产品', value: 5},
            {label: '安卓门禁产品', value: 6},
            {label: '门铃', value: 7},
            {label: '4G开门控制器',value: 8},
            {label: 'linux人脸机',value: 12}
          ]
        }
      },
      {
        index: 'deviceLocation',
        title: '安装地址',
        width:350,
        filter: {
          placeholder: '请输入安装地址',
          type: 'string'
        },
        //className:'w200'
      },
      {
        index: 'deviceLocationStatus',
        title: '绑定状态',
        type: ColumnType.tag,
        tag: {
          0:{text: '未绑定',color: 'red'},
          1:{text: '已绑定',color: 'green'}
        },
        filter: {
          placeholder: '请选择绑定状态',
          type: 'select',
          items: [
            {label: '未绑定', value: 0},
            {label: '已绑定', value: 1},
          ]
        }
      },
      {
        index: 'deviceLastStatus',
        title: '设备状态',
        type: ColumnType.tag,
        tag:{
          0:{text: '离线',color: 'red'},
          1:{text: '在线',color: 'green'}
        },
        filter: {
          placeholder: '请选择设备状态',
          type: 'select',
          items: [
            {label: '离线', value: 0},
            {label: '在线', value: 1},
          ]
        }
      },
      {
        index: 'deviceIp',
        title: '门禁IP',
        filter:{
          placeholder:'请输入设备IP地址',
          type:'string'
        }
      },
      {
        index: 'status',
        title: '状态',
        type: ColumnType.tag,
        tag:{
          true:{text:'启用',color:'green'},
          false:{text:'停用',color:'red'}
        }
      },
      {
        index: '',
        title: '操作',
        width:200,
        fixed:'right',
        buttons:[
          {
            text:'更换机器',
            iif: (data: STData) => {
              return data.deviceLocationStatus == 1 && this.changeFlag;
            },
            click:(_record)=>{
              this.editDic(1,_record);
            }
          },
          {
            text:'解绑',
            iif: (data: STData) => {
              return data.deviceLocationStatus == 1 && this.unbundingFlag;
            },
            click:(_record)=>{
              this.disAble(_record.id,_record.areaId);
            }
          },
          {
            text:'同步时间',
            iif:()=>{
              return this.syncFlag;
            },
            click:(_record)=>{
              this.setTimeSync(_record.deviceCode);
            }
          }
        ]
      },
    ],
    showCheckBox: false,
    reqUrl:'/core/host/page',
    // editBtn: [
    //   { title: '更换机器' ,
    //   iif: (data: STData) => {
    //     return data.deviceLocationStatus == 1
    //   }},
    //   {title: '解绑',
    //   iif: (data: STData) => {
    //     return data.deviceLocationStatus == 1
    //   }}
    // ]
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private aclService: ACLService,) { }
  
  ngOnInit() {

  }

  //同步时间
  setTimeSync(sn:string){
    this.modal.confirm({
      nzTitle:"确定同步?",
      nzOnOk:()=>{
        this.http.post(this.api.device.issueTime.url,{deviceSn:sn}).subscribe(res=>{
          if(res.status == 200){
            this.message.success(res.message);
          }else{
            this.message.error(res.message);
          }
        })
      }
    })
  
  }


  // 编辑功能开始
  editDic(type: 0 | 1 = 0, _param?: object): void {
    console.log(type);
    const title = '设备换机';
    const modal = this.modal.create({
      nzTitle: title,
      nzContent: ChangeEditComponent,
      nzWidth: 1000,
      nzComponentParams: {
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }
  //解绑
  disAble(param:number,id:string):void{
    console.log(param);
    this.modal.confirm({
      nzTitle: '解绑后门禁权限会删除，是否确认解除绑定？',
      nzOnOk: () => {
        const body = {deviceId:param,areaId:id};
        this.http.post('/core/host/hostUntie',body).subscribe(res => {
          if(res.status === '200'){
            this.message.success('解绑成功');
            this.st.reload();
          }else{
            this.message.warning(res.message)
          }
        })
      }
    })
    
  }
  operate(e: any): void {
    if (e.title === '更换机器') {
      this.editDic(1, e.data);
    }
    // if(e.title === '解绑'){
    //   this.disAble(e.data.id);
    // }
  }
  _console(e: any): void {
    this.selectData = e;
  }

  //清空白名单
  remove(){
    if(this.selectData == undefined){
      this.message.error("请选择设备!");
      return;
    }
    console.log(this.selectData)
    const body:any={
      deviceCode:this.selectData.deviceCode,
      deviceType:this.selectData.deviceType,
      id:this.selectData.id
    }
    if(this.selectData.deviceType == 2){
      body.deviceNetworkingWay = "http"
    }
    console.log(body)
    this.modal.confirm({
      nzTitle:"确定清空白名单?",
      nzOnOk: ()=>{
        this.http.post("/core/host/deleteCardAll",body).subscribe((res:any) => {
          if (res.status === '200') {
            this.message.success(res.message);
            this.st.reload();
          }else{
            this.message.error(res.message);
          }
        })
      }
    })
  }
  //下发白名单
  issue(){
    if(this.selectData == undefined){
      this.message.error("请选择设备!");
      return;
    }
    const body:any={
      deviceCode:this.selectData.deviceCode,
      deviceType:this.selectData.deviceType,
      id:this.selectData.id
    }
    if(this.selectData.deviceType == 2){
      body.deviceNetworkingWay = "http"
    }
    this.modal.confirm({
      nzTitle:"确定下发白名单?",
      nzOnOk: ()=>{
        this.http.post("/core/host/sendCardAll",body).subscribe((res:any) => {
          if (res.status === '200') {
            this.message.success(res.message);
            this.st.reload();
          }else{
            this.message.error(res.message);
          }
        })
      }
    })
  }
   //清空人脸
   removeFace(){
    if(this.selectData == undefined){
      this.message.error("请选择设备!");
      return;
    }
    const body:any={
      deviceCode:this.selectData.deviceCode,
      deviceType:this.selectData.deviceType,
      id:this.selectData.id
    }
    if(this.selectData.deviceType == 2){
      body.deviceNetworkingWay = "http"
    }
    this.modal.confirm({
      nzTitle:"确定清空人脸?",
      nzOnOk: ()=>{
        this.http.post("/core/host/deleteFaceAll",body).subscribe((res:any) => {
          if (res.status === '200') {
            this.message.success(res.message);
            this.st.reload();
          }else{
            this.message.error(res.message);
          }
        })
      }
    })
  }
   //下发人脸
   issueFace(){
    if(this.selectData == undefined){
      this.message.error("请选择设备!");
      return;
    }
    const body:any={
      deviceCode:this.selectData.deviceCode,
      deviceType:this.selectData.deviceType,
      id:this.selectData.id
    }
    if(this.selectData.deviceType == 2){
      body.deviceNetworkingWay = "http"
    }
    this.modal.confirm({
      nzTitle:"确定下发人脸?",
      nzOnOk: ()=>{
        this.http.post("/core/host/sendFaceAll",body).subscribe((res:any) => {
          if (res.status === '200') {
            this.message.success(res.message);
            this.st.reload();
          }else{
            this.message.error(res.message);
          }
        })
      }
    })
  }

   //远程重启
   restart(){
    if(this.selectData == undefined){
      this.message.error("请选择设备!");
      return;
    }    
    console.log(this.selectData);
    this.modal.confirm({
      nzTitle:"确定重启?",
      nzOnOk: ()=>{
        this.http.post("/core/host/restart",{deviceCode:this.selectData.deviceCode,deviceType:this.selectData.deviceType,id:this.selectData.id}).subscribe((res:any) => {
          if (res.status === '200') {
            this.message.success(res.message);
            this.st.reload();
          }else{
            this.message.error(res.message);
          }
        })
      }
    })
  }
  //远程开门
  openDoor(){
    if(this.selectData == undefined){
      this.message.error("请选择设备!");
      return;
    }    
    this.modal.confirm({
      nzTitle:"确定开门?",
      nzOnOk:()=>{
        this.http.post("/core/host/remoteOpen",{deviceCode:this.selectData.deviceCode,deviceType:this.selectData.deviceType,id:this.selectData.id}).subscribe((res:any) => {
          if (res.status === '200') {
            this.message.success(res.message);
          }else{
            this.message.error(res.message);
          }
        })
      }
    })
  }
  

}
